package com.lottery.im.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lottery.im.R;
import com.lottery.im.util.AnimTools;

public class ShipVideoView extends RelativeLayout {

  private float startCar1, startCar2, startCar3, startCar4, startCar5, startCar6, startCar7,
      startCar8, startCar9, startCar10;
  private boolean isShow = false;

  private ScrollingImageView mScrollingImageView;
  private ScrollingImageView mScrollingImageView2;
  private TextView mTvStatus;

  private View mLayoutPkAnim;
  private View mLayoutResult;

  private View mLayoutCar1;
  private View mLayoutCar2;
  private View mLayoutCar3;
  private View mLayoutCar4;
  private View mLayoutCar5;
  private View mLayoutCar6;
  private View mLayoutCar7;
  private View mLayoutCar8;
  private View mLayoutCar9;
  private View mLayoutCar10;

  private ImageView mIvWater1;
  private ImageView mIvWater2;
  private ImageView mIvWater3;
  private ImageView mIvWater4;
  private ImageView mIvWater5;
  private ImageView mIvWater6;
  private ImageView mIvWater7;
  private ImageView mIvWater8;
  private ImageView mIvWater9;
  private ImageView mIvWater10;

  private ImageView mIvBehind1;
  private ImageView mIvBehind2;
  private ImageView mIvBehind3;
  private ImageView mIvBehind4;
  private ImageView mIvBehind5;
  private ImageView mIvBehind6;
  private ImageView mIvBehind7;
  private ImageView mIvBehind8;
  private ImageView mIvBehind9;
  private ImageView mIvBehind10;

  private ImageView mIvFir1;
  private ImageView mIvFir2;
  private ImageView mIvFir3;
  private ImageView mIvFir4;
  private ImageView mIvFir5;
  private ImageView mIvFir6;
  private ImageView mIvFir7;
  private ImageView mIvFir8;
  private ImageView mIvFir9;
  private ImageView mIvFir10;

  private ImageView mIvResult1;
  private ImageView mIvResult2;
  private ImageView mIvResult3;
  private ImageView mIvResult4;
  private ImageView mIvResult5;
  private ImageView mIvResult6;
  private ImageView mIvResult7;
  private ImageView mIvResult8;
  private ImageView mIvResult9;
  private ImageView mIvResult10;

  private ImageView mIvWinner1;
  private ImageView mIvWinner2;
  private ImageView mIvWinner3;
  private ImageView mIvWinner4;
  private ImageView mIvWinner5;
  private ImageView mIvWinner6;
  private ImageView mIvWinner7;
  private ImageView mIvWinner8;
  private ImageView mIvWinner9;
  private ImageView mIvWinner10;

  public ShipVideoView(Context context) {
    this(context, null);
  }

  public ShipVideoView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public ShipVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  private void init() {
    // 开启硬件加速
    setLayerType(View.LAYER_TYPE_SOFTWARE, null);

    View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_ship_video, this);
    view.findViewById(R.id.tv_close).setOnClickListener(v -> {
      animGone();
      isShow = false;
    });

    mScrollingImageView = view.findViewById(R.id.ScrollingImageView);
    mScrollingImageView2 = view.findViewById(R.id.ScrollingImageView2);
    mTvStatus = view.findViewById(R.id.tv_status);

    mLayoutPkAnim = view.findViewById(R.id.layout_pk_anim);
    mLayoutResult = view.findViewById(R.id.layout_result);

    mLayoutCar1 = view.findViewById(R.id.layout_car1);
    mLayoutCar2 = view.findViewById(R.id.layout_car2);
    mLayoutCar3 = view.findViewById(R.id.layout_car3);
    mLayoutCar4 = view.findViewById(R.id.layout_car4);
    mLayoutCar5 = view.findViewById(R.id.layout_car5);
    mLayoutCar6 = view.findViewById(R.id.layout_car6);
    mLayoutCar7 = view.findViewById(R.id.layout_car7);
    mLayoutCar8 = view.findViewById(R.id.layout_car8);
    mLayoutCar9 = view.findViewById(R.id.layout_car9);
    mLayoutCar10 = view.findViewById(R.id.layout_car10);

    mIvWater1 = view.findViewById(R.id.iv_water1);
    mIvWater2 = view.findViewById(R.id.iv_water2);
    mIvWater3 = view.findViewById(R.id.iv_water3);
    mIvWater4 = view.findViewById(R.id.iv_water4);
    mIvWater5 = view.findViewById(R.id.iv_water5);
    mIvWater6 = view.findViewById(R.id.iv_water6);
    mIvWater7 = view.findViewById(R.id.iv_water7);
    mIvWater8 = view.findViewById(R.id.iv_water8);
    mIvWater9 = view.findViewById(R.id.iv_water9);
    mIvWater10 = view.findViewById(R.id.iv_water10);

    mIvBehind1 = view.findViewById(R.id.iv_behind1);
    mIvBehind2 = view.findViewById(R.id.iv_behind2);
    mIvBehind3 = view.findViewById(R.id.iv_behind3);
    mIvBehind4 = view.findViewById(R.id.iv_behind4);
    mIvBehind5 = view.findViewById(R.id.iv_behind5);
    mIvBehind6 = view.findViewById(R.id.iv_behind6);
    mIvBehind7 = view.findViewById(R.id.iv_behind7);
    mIvBehind8 = view.findViewById(R.id.iv_behind8);
    mIvBehind9 = view.findViewById(R.id.iv_behind9);
    mIvBehind10 = view.findViewById(R.id.iv_behind10);

    mIvFir1 = view.findViewById(R.id.iv_fir1);
    mIvFir2 = view.findViewById(R.id.iv_fir2);
    mIvFir3 = view.findViewById(R.id.iv_fir3);
    mIvFir4 = view.findViewById(R.id.iv_fir4);
    mIvFir5 = view.findViewById(R.id.iv_fir5);
    mIvFir6 = view.findViewById(R.id.iv_fir6);
    mIvFir7 = view.findViewById(R.id.iv_fir7);
    mIvFir8 = view.findViewById(R.id.iv_fir8);
    mIvFir9 = view.findViewById(R.id.iv_fir9);
    mIvFir10 = view.findViewById(R.id.iv_fir10);

    mIvResult1 = view.findViewById(R.id.iv_result1);
    mIvResult2 = view.findViewById(R.id.iv_result2);
    mIvResult3 = view.findViewById(R.id.iv_result3);
    mIvResult4 = view.findViewById(R.id.iv_result4);
    mIvResult5 = view.findViewById(R.id.iv_result5);
    mIvResult6 = view.findViewById(R.id.iv_result6);
    mIvResult7 = view.findViewById(R.id.iv_result7);
    mIvResult8 = view.findViewById(R.id.iv_result8);
    mIvResult9 = view.findViewById(R.id.iv_result9);
    mIvResult10 = view.findViewById(R.id.iv_result10);

    mIvWinner1 = view.findViewById(R.id.iv_winner1);
    mIvWinner2 = view.findViewById(R.id.iv_winner2);
    mIvWinner3 = view.findViewById(R.id.iv_winner3);
    mIvWinner4 = view.findViewById(R.id.iv_winner4);
    mIvWinner5 = view.findViewById(R.id.iv_winner5);
    mIvWinner6 = view.findViewById(R.id.iv_winner6);
    mIvWinner7 = view.findViewById(R.id.iv_winner7);
    mIvWinner8 = view.findViewById(R.id.iv_winner8);
    mIvWinner9 = view.findViewById(R.id.iv_winner9);
    mIvWinner10 = view.findViewById(R.id.iv_winner10);
  }

  /**
   * 开奖倒计时
   *
   * @param time 倒计时
   */
  public void setWaiting(String time) {
    mTvStatus.setText(time);
  }

  public void setWaitVisible(boolean isVisible) {
    if (isVisible) {
      mTvStatus.setVisibility(View.VISIBLE);
    } else {
      mTvStatus.setVisibility(View.GONE);
    }
  }

  public boolean isShow() {
    return isShow;
  }

  /**
   * 正在开奖
   */
  public void setRunning(boolean isBegin) {
    if (isBegin) {
      mIvWater1.setVisibility(View.VISIBLE);
      mIvWater2.setVisibility(View.VISIBLE);
      mIvWater3.setVisibility(View.VISIBLE);
      mIvWater4.setVisibility(View.VISIBLE);
      mIvWater5.setVisibility(View.VISIBLE);
      mIvWater6.setVisibility(View.VISIBLE);
      mIvWater7.setVisibility(View.VISIBLE);
      mIvWater8.setVisibility(View.VISIBLE);
      mIvWater9.setVisibility(View.VISIBLE);
      mIvWater10.setVisibility(View.VISIBLE);
      mIvBehind1.setVisibility(View.VISIBLE);
      mIvBehind2.setVisibility(View.VISIBLE);
      mIvBehind3.setVisibility(View.VISIBLE);
      mIvBehind4.setVisibility(View.VISIBLE);
      mIvBehind5.setVisibility(View.VISIBLE);
      mIvBehind6.setVisibility(View.VISIBLE);
      mIvBehind7.setVisibility(View.VISIBLE);
      mIvBehind8.setVisibility(View.VISIBLE);
      mIvBehind9.setVisibility(View.VISIBLE);
      mIvBehind10.setVisibility(View.VISIBLE);
      mScrollingImageView.start();
      mScrollingImageView2.start();
      mScrollingImageView2.setVisibility(View.GONE);
      float position1 = (float) -(0.5 + (Math.random()));
      float position2 = (float) -(0.5 + (Math.random()));
      float position3 = (float) -(0.5 + (Math.random()));
      float position4 = (float) -(0.5 + (Math.random()));
      float position5 = (float) -(0.5 + (Math.random()));
      float position6 = (float) -(0.5 + (Math.random()));
      float position7 = (float) -(0.5 + (Math.random()));
      float position8 = (float) -(0.5 + (Math.random()));
      float position9 = (float) -(0.5 + (Math.random()));
      float position10 = (float) -(0.5 + (Math.random()));
      setCarAnim(position1, position2, position3, position4, position5, position6, position7,
          position8, position9, position10);
    } else {
      mIvWater1.setVisibility(View.GONE);
      mIvWater2.setVisibility(View.GONE);
      mIvWater3.setVisibility(View.GONE);
      mIvWater4.setVisibility(View.GONE);
      mIvWater5.setVisibility(View.GONE);
      mIvWater6.setVisibility(View.GONE);
      mIvWater7.setVisibility(View.GONE);
      mIvWater8.setVisibility(View.GONE);
      mIvWater9.setVisibility(View.GONE);
      mIvWater10.setVisibility(View.GONE);
      mIvBehind1.setVisibility(View.GONE);
      mIvBehind2.setVisibility(View.GONE);
      mIvBehind3.setVisibility(View.GONE);
      mIvBehind4.setVisibility(View.GONE);
      mIvBehind5.setVisibility(View.GONE);
      mIvBehind6.setVisibility(View.GONE);
      mIvBehind7.setVisibility(View.GONE);
      mIvBehind8.setVisibility(View.GONE);
      mIvBehind9.setVisibility(View.GONE);
      mIvBehind10.setVisibility(View.GONE);
      mScrollingImageView2.setVisibility(View.VISIBLE);
      mScrollingImageView.stop();
      mScrollingImageView2.stop();
      setCarAnim(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    }
  }

  public void changeVisible() {
    if (!isShow) {
      animVisible();
    } else {
      animGone();
    }
  }

  public void animVisible() {
    AnimTools.anim(mLayoutPkAnim, 0,
        (int) getResources().getDimension(R.dimen.ship_anim_height),
        getResources().getInteger(R.integer.anim_duration));
    isShow = true;
  }

  public void animGone() {
    AnimTools.anim(mLayoutPkAnim,
        (int) getResources().getDimension(R.dimen.ship_anim_height),
        0,
        getResources().getInteger(R.integer.anim_duration));
    isShow = false;
  }

  private void setCarAnim(float position1, float position2, float position3, float position4,
      float position5, float position6, float position7, float position8, float position9,
      float position10) {
    if (startCar1 > position1) {
      mIvFir1.setVisibility(View.VISIBLE);
    } else {
      mIvFir1.setVisibility(View.GONE);
    }
    if (startCar2 > position2) {
      mIvFir2.setVisibility(View.VISIBLE);
    } else {
      mIvFir2.setVisibility(View.GONE);
    }
    if (startCar3 > position3) {
      mIvFir3.setVisibility(View.VISIBLE);
    } else {
      mIvFir3.setVisibility(View.GONE);
    }
    if (startCar4 > position4) {
      mIvFir4.setVisibility(View.VISIBLE);
    } else {
      mIvFir4.setVisibility(View.GONE);
    }
    if (startCar5 > position5) {
      mIvFir5.setVisibility(View.VISIBLE);
    } else {
      mIvFir5.setVisibility(View.GONE);
    }
    if (startCar6 > position6) {
      mIvFir6.setVisibility(View.VISIBLE);
    } else {
      mIvFir6.setVisibility(View.GONE);
    }
    if (startCar7 > position7) {
      mIvFir7.setVisibility(View.VISIBLE);
    } else {
      mIvFir7.setVisibility(View.GONE);
    }
    if (startCar8 > position8) {
      mIvFir8.setVisibility(View.VISIBLE);
    } else {
      mIvFir8.setVisibility(View.GONE);
    }
    if (startCar9 > position9) {
      mIvFir9.setVisibility(View.VISIBLE);
    } else {
      mIvFir9.setVisibility(View.GONE);
    }
    if (startCar10 > position10) {
      mIvFir10.setVisibility(View.VISIBLE);
    } else {
      mIvFir10.setVisibility(View.GONE);
    }
    mLayoutCar1.startAnimation(AnimTools.getAnimSet(startCar1, position1));
    mLayoutCar2.startAnimation(AnimTools.getAnimSet(startCar2, position2));
    mLayoutCar3.startAnimation(AnimTools.getAnimSet(startCar3, position3));
    mLayoutCar4.startAnimation(AnimTools.getAnimSet(startCar4, position4));
    mLayoutCar5.startAnimation(AnimTools.getAnimSet(startCar5, position5));
    mLayoutCar6.startAnimation(AnimTools.getAnimSet(startCar6, position6));
    mLayoutCar7.startAnimation(AnimTools.getAnimSet(startCar7, position7));
    mLayoutCar8.startAnimation(AnimTools.getAnimSet(startCar8, position8));
    mLayoutCar9.startAnimation(AnimTools.getAnimSet(startCar9, position9));
    mLayoutCar10.startAnimation(AnimTools.getAnimSet(startCar10, position10));
    startCar1 = position1;
    startCar2 = position2;
    startCar3 = position3;
    startCar4 = position4;
    startCar5 = position5;
    startCar6 = position6;
    startCar7 = position7;
    startCar8 = position8;
    startCar9 = position9;
    startCar10 = position10;
  }

  /**
   * 颁奖台显示,第一名
   *
   * @param resultStr 开奖结果
   */
  public void setResult(String resultStr) {
    AnimTools.anim(mLayoutResult, 0,
        (int) getResources().getDimension(R.dimen.ship_anim_height),
        getResources().getInteger(R.integer.anim_duration));
    String[] str = resultStr.split(",");
    if (str.length >= 10) {
      mIvWinner1.setImageResource(getResourceId("winner_ship" + str[0]));
      mIvWinner2.setImageResource(getResourceId("winner_ship" + str[1]));
      mIvWinner3.setImageResource(getResourceId("winner_ship" + str[2]));
      mIvWinner4.setImageResource(getResourceId("winner_ship" + str[3]));
      mIvWinner5.setImageResource(getResourceId("winner_ship" + str[4]));
      mIvWinner6.setImageResource(getResourceId("winner_ship" + str[5]));
      mIvWinner7.setImageResource(getResourceId("winner_ship" + str[6]));
      mIvWinner8.setImageResource(getResourceId("winner_ship" + str[7]));
      mIvWinner9.setImageResource(getResourceId("winner_ship" + str[8]));
      mIvWinner10.setImageResource(getResourceId("winner_ship" + str[9]));
      mIvWinner1.startAnimation(AnimTools.getAnimSet(-0.5f, 0, -0.5f, 0));
      mIvResult1.startAnimation(AnimTools.getAnimSet());
      mIvWinner1.setVisibility(View.VISIBLE);
      mIvResult1.setVisibility(View.VISIBLE);
    }
  }

  /**
   * 第二名
   */
  public void setResult2() {
    mIvWinner2.startAnimation(AnimTools.getAnimSet(-0.5f, 0, -0.5f, 0));
    mIvResult2.startAnimation(AnimTools.getAnimSet());
    mIvWinner2.setVisibility(View.VISIBLE);
    mIvResult2.setVisibility(View.VISIBLE);
  }

  /**
   * 第三名
   */
  public void setResult3() {
    mIvWinner3.startAnimation(AnimTools.getAnimSet(-0.5f, 0, -0.5f, 0));
    mIvResult3.startAnimation(AnimTools.getAnimSet());
    mIvWinner3.setVisibility(View.VISIBLE);
    mIvResult3.setVisibility(View.VISIBLE);
  }

  /**
   * 第四名-第十名
   */
  public void setResult4() {
    mIvWinner4.startAnimation(AnimTools.getAnimSet(-0.5f, 0, -0.5f, 0));
    mIvWinner5.startAnimation(AnimTools.getAnimSet(-0.5f, 0, -0.5f, 0));
    mIvWinner6.startAnimation(AnimTools.getAnimSet(-0.5f, 0, -0.5f, 0));
    mIvWinner7.startAnimation(AnimTools.getAnimSet(-0.5f, 0, -0.5f, 0));
    mIvWinner8.startAnimation(AnimTools.getAnimSet(-0.5f, 0, -0.5f, 0));
    mIvWinner9.startAnimation(AnimTools.getAnimSet(-0.5f, 0, -0.5f, 0));
    mIvWinner10.startAnimation(AnimTools.getAnimSet(-0.5f, 0, -0.5f, 0));
    mIvResult4.startAnimation(AnimTools.getAnimSet());
    mIvResult5.startAnimation(AnimTools.getAnimSet());
    mIvResult6.startAnimation(AnimTools.getAnimSet());
    mIvResult7.startAnimation(AnimTools.getAnimSet());
    mIvResult8.startAnimation(AnimTools.getAnimSet());
    mIvResult9.startAnimation(AnimTools.getAnimSet());
    mIvResult10.startAnimation(AnimTools.getAnimSet());
    mIvWinner4.setVisibility(View.VISIBLE);
    mIvWinner5.setVisibility(View.VISIBLE);
    mIvWinner6.setVisibility(View.VISIBLE);
    mIvWinner7.setVisibility(View.VISIBLE);
    mIvWinner8.setVisibility(View.VISIBLE);
    mIvWinner9.setVisibility(View.VISIBLE);
    mIvWinner10.setVisibility(View.VISIBLE);
    mIvResult4.setVisibility(View.VISIBLE);
    mIvResult5.setVisibility(View.VISIBLE);
    mIvResult6.setVisibility(View.VISIBLE);
    mIvResult7.setVisibility(View.VISIBLE);
    mIvResult8.setVisibility(View.VISIBLE);
    mIvResult9.setVisibility(View.VISIBLE);
    mIvResult10.setVisibility(View.VISIBLE);
  }

  /**
   * 颁奖台消失
   */
  public void setResultGone() {
    AnimTools.anim(mLayoutResult, (int) getResources().getDimension(R.dimen.ship_anim_height),
        0,
        getResources().getInteger(R.integer.anim_duration));
    mIvWinner1.clearAnimation();
    mIvWinner2.clearAnimation();
    mIvWinner3.clearAnimation();
    mIvWinner4.clearAnimation();
    mIvWinner5.clearAnimation();
    mIvWinner6.clearAnimation();
    mIvWinner7.clearAnimation();
    mIvWinner8.clearAnimation();
    mIvWinner9.clearAnimation();
    mIvWinner10.clearAnimation();
    mIvWinner1.setVisibility(View.GONE);
    mIvWinner2.setVisibility(View.GONE);
    mIvWinner3.setVisibility(View.GONE);
    mIvWinner4.setVisibility(View.GONE);
    mIvWinner5.setVisibility(View.GONE);
    mIvWinner6.setVisibility(View.GONE);
    mIvWinner7.setVisibility(View.GONE);
    mIvWinner8.setVisibility(View.GONE);
    mIvWinner9.setVisibility(View.GONE);
    mIvWinner10.setVisibility(View.GONE);
    mIvResult1.setVisibility(View.GONE);
    mIvResult2.setVisibility(View.GONE);
    mIvResult3.setVisibility(View.GONE);
    mIvResult4.setVisibility(View.GONE);
    mIvResult5.setVisibility(View.GONE);
    mIvResult6.setVisibility(View.GONE);
    mIvResult7.setVisibility(View.GONE);
    mIvResult8.setVisibility(View.GONE);
    mIvResult9.setVisibility(View.GONE);
    mIvResult10.setVisibility(View.GONE);
  }

  //根据图片名称取得对应图片
  public int getResourceId(String name) {
    int resId =
        getContext().getResources().getIdentifier(name, "drawable", getContext().getPackageName());
    //解析资源文件夹下，id为resID的图片
    return resId;
  }
}
